package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.constant.BusinessConstant;
import com.example.mybatisplus.mapper.ConfigMapper;
import com.example.mybatisplus.model.entity.Config;
import com.example.mybatisplus.model.param.ConfigParam;
import com.example.mybatisplus.model.query.ConfigQuery;
import com.example.mybatisplus.model.result.ConfigResult;
import com.example.mybatisplus.service.ConfigService;

import com.example.mybatisplus.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统参数(Config)表服务实现类
 *
 * @author 宋义州
 * @since 2023-09-02 09:21:44
 */
@Service
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Override
    public void insertSelective(ConfigParam param) {
        Config config = BeanUtils.copyProperties(param, Config.class);
        this.baseMapper.insert(config);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        //先查询出该条数据
        Config config = this.baseMapper.selectOne(new LambdaQueryWrapper<Config>()
                .eq(Config::getId, id)
                .eq(Config::getDelFlag, BusinessConstant.DeleteStatus.NO_DELETE));
        //标记位改为已删除
        config.setDelFlag(BusinessConstant.DeleteStatus.DELETE);
        this.baseMapper.updateById(config);
    }

    @Override
    public void updateByPrimaryKeySelective(ConfigParam param) {
        Config config = BeanUtils.copyProperties(param, Config.class);
        this.baseMapper.updateById(config);
    }

    @Override
    public ConfigResult selectByPrimaryKey(Long id) {
        return this.baseMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<ConfigResult> selectByPage(ConfigQuery param) {
        Page<ConfigResult> page = new Page<>(param.getPageNum(), param.getPageSize());
        page = this.baseMapper.selectByPage(page, param);
        return page;
    }
}

